package com.chainyoung.common.utils;

import android.text.TextUtils;

import com.chainyoung.common.base.BaseResponse;
import com.chainyoung.common.exception.ApiException;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Completable;
import io.reactivex.CompletableTransformer;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;
import okhttp3.ResponseBody;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

public class RxUtils {

    /**
     * Flowable线程切换简化
     */
    public static <T> FlowableTransformer<T, T> rxSchedulerHelper() {    //compose简化线程
        return new FlowableTransformer<T, T>() {
            @Override
            public Flowable<T> apply(Flowable<T> observable) {
                return observable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }


    /**
     * Completable线程切换简化
     */
    public static CompletableTransformer completableSchedulerHelper() {
        return new CompletableTransformer() {
            @Override
            public Completable apply(Completable observable) {
                return observable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }

        };
    }

    /**
     * 统一返回结果处理
     *
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<BaseResponse<T>, T> handleResult() {   //compose判断结果
        return new FlowableTransformer<BaseResponse<T>, T>() {
            @Override
            public Flowable<T> apply(Flowable<BaseResponse<T>> httpResponseFlowable) {
                return httpResponseFlowable.flatMap(new Function<BaseResponse<T>, Flowable<T>>() {
                    @Override
                    public Flowable<T> apply(BaseResponse<T> tHttpResponse) {
                        if (tHttpResponse.getCode()==200&&(TextUtils.isEmpty(tHttpResponse.getError_code()))) {
                            // TODO: 2018/8/4 此处若tHttpResponse.getData()==null rxjava2 onnext不支持null 会报错需要处理
                            if (tHttpResponse.getData() == null) {
                                return Flowable.error(new ApiException(NO_RESULT_ERRORCODE, "result is null"));
                            } else {
                                return createData(tHttpResponse.getData());
                            }
                        } else {
                            return Flowable.error(new ApiException(TextUtils.isEmpty(tHttpResponse.getError_code()) ? "-1" : tHttpResponse.getError_code(), tHttpResponse.getError()));
                        }
                    }
                });
            }
        };
    }









    /**
     * 生成Flowable
     */
    public static <T> Flowable<T> createData(final T t) {
        return Flowable.create(new FlowableOnSubscribe<T>() {
            @Override
            public void subscribe(FlowableEmitter<T> emitter) throws Exception {
                try {
                    emitter.onNext(t);
                    emitter.onComplete();
                } catch (Exception e) {
                    emitter.onError(e);
                }
            }
        }, BackpressureStrategy.BUFFER);
    }

}
